//
//  NewsDetailHttpMode..swift
//  MyTouTiao
//
//  Created by Yuri on 2018/3/19.
//  Copyright © 2018年 Yuri. All rights reserved.
//

import Foundation
import ObjectMapper
import Moya

class NewsListHttpModel : Mappable, CustomStringConvertible {
    
    var status : String!
    var error : String?
    var count : Int?
    var pages : Int?
    
    var category : HttpCategory?
    var posts : [HttpPost]?
    
    required init?(map: Map) {
        
    }
    
    func mapping(map: Map) {
        status <- map["status"]
        error <- map["error"]
        count <- map["count"]
        pages <- map["pages"]
        category <- map["category"]
        posts <- map["posts"]
    }
    
    var description: String {
        return "NewsListHttpModel:{"
            + "status=\(status!)"
            + ",error=\(error)"
            + ",count=\(count)"
            + ",pages=\(pages)"
            + ",category=\(category)"
            + ",posts=\(posts)"
            + "}"
    }
    
}

class SubmitCommentResponse : Mappable, CustomStringConvertible {
    
    var status : String!
    var error : String?
    
    required init?(map: Map) {
        
    }
    
    func mapping(map: Map) {
        status <- map["status"]
        error <- map["error"]
    }
    
    var description: String {
        return "SubmitCommentResponse:{"
            + "status=\(status!)"
            + ",error=\(error)"
            + "}"
    }
    
}

class HttpPost: Mappable, CustomStringConvertible {
    var id : Int!
    var type : String!
    var slug : String!
    var url : String!
    var status : String!
    var title : String!
    var titlePlain : String!
    var content : String!
    var excerpt : String!
    var date : String!
    var modified : String!
    var categories : [HttpCategory]!
    var tags : [HttpTag]!
    var author : HttpAuthor!
    var comments: [HttpComment]!
    //TODO 附件（后面再添加）
//    var attachments :[String]
    var commentCount : Int!
    //评论状态： open/close
    var commentStatus : String!
    //TODO 暂时还不知道这个是干什么的，自定义的吧
//    var custom_fields : String!
    //缩略图，可能为空
    var thumbnail : String?

    
    required init?(map: Map) {
        //这个一般什么都不用做
    }
    
    func mapping(map: Map) {
        id <- map["id"]
        type <- map["type"]
        slug <- map["slug"]
        url <- map["url"]
        status <- map["status"]
        title <- map["title"]
        titlePlain <- map["title_plain"]
        content <- map["content"]
        excerpt <- map["excerpt"]
        
        date <- map["date"]
        modified <- map["modified"]
        categories <- map["categories"]
        tags <- map["tags"]
        author <- map["author"]
        comments <- map["comments"]
        commentCount <- map["comment_count"]
        commentStatus <- map["comment_status"]
        thumbnail <- map["thumbnail"]
    }
    
    var description: String {
        return "HttpCategory:{"
            + "id=\(id!)"
            + "type=\(type)"
            + ",slug=\(slug!)"
            + ",url=\(url!)"
            + ",status=\(status!)"
            + ",title=\(title!)"
            + ",titlePlain=\(titlePlain!)"
            + ",content=\(content!)"
            + ",excerpt=\(excerpt!)"
            + ",date=\(date!)"
            + ",modified=\(modified!)"
            + ",categories=\(categories!)"
            + ",tags=\(tags!)"
            + ",author=\(author!)"
            + ",comments=\(comments!)"
            + ",commentCount=\(commentCount!)"
            + ",commentStatus=\(commentStatus!)"
            + ",thumbnail=\(thumbnail)"
            + "}"
    }
}

//文章标签
class HttpTag : Mappable, CustomStringConvertible {
    var id : Int!
    var slug : String!
    var title : String!
    var description1 : String!
    var count : Int!
    
    required init?(map: Map) {
        //这个一般什么都不用做
    }
    
    func mapping(map: Map) {
        id <- map["id"]
        slug <- map["slug"]
        title <- map["title"]
        description1 <- map["description"]
        count <- map["post_count"]
    }
    
    var description: String {
        return "HttpTag:{"
            + "id=\(id!)"
            + ",slug=\(slug!)"
            + ",title=\(title!)"
            + ",description=\(description1!)"
            + ",count=\(count!)"
            + "}"
    }
}

//文章作者
class HttpAuthor : Mappable, CustomStringConvertible {
    var id : Int!
    var slug : String!
    var name : String!
    var firstName : String!
    var lastName : String!
    var nickname : String!
    var description1 : String!
    var url : String!
    
    required init?(map: Map) {
        //这个一般什么都不用做
    }
    
    func mapping(map: Map) {
        id <- map["id"]
        slug <- map["slug"]
        name <- map["name"]
        firstName <- map["first_name"]
        lastName <- map["last_name"]
        nickname <- map["nickname"]
        description1 <- map["description"]
        url <- map["url"]
    }
    
    var description: String {
        return "HttpAuthor:{"
            + "id=\(id!)"
            + ",name=\(name!)"
            + ",firstName=\(firstName!)"
            + ",lastName=\(lastName!)"
            + ",nickname=\(nickname!)"
            + ",description=\(description1!)"
            + ",url=\(url!)"
            + "}"
    }
}

//文章评论
class HttpComment: Mappable, CustomStringConvertible {
    var id : Int!
    var name : String!
    var date : String!
    var content : String!
    var parent : Int!
    var url : String!
    
    required init?(map: Map) {
        //这个一般什么都不用做
    }
    
    func mapping(map: Map) {
        id <- map["id"]
        name <- map["name"]
        date <- map["date"]
        content <- map["content"]
        parent <- map["parent"]
        url <- map["url"]
    }
    
    var description: String {
        return "HttpAuthor:{"
            + "id=\(id!)"
            + ",name=\(name!)"
            + ",date=\(date!)"
            + ",content=\(content!)"
            + ",parent=\(parent!)"
            + ",url=\(url!)"
            + "}"
    }
}

extension NewsListHttpModel {
    static func getCategoryPost(id: Int, completion: @escaping (NewsListHttpModel?) -> Void) {
        let provider = MoyaProvider<ApiService>()
        provider.request(.categoryPost(id: id)) { (result) in
            switch result {
            case let .success(moyaResponse) :
                let json = try! moyaResponse.mapJSON() as! [String:Any]
                if let response = NewsListHttpModel(JSON: json) {
                    print(response)
                    completion(response)
                }
            case let .failure(error) :
                print("请求网络错误.:\(error.failureReason ?? "")")
                completion(nil)
            }
        }
        
    }
    
    //提交评论
    static func submitComment(postId: Int, name : String, email : String, content : String, completion: @escaping (Bool) -> Void) {
        let provider = MoyaProvider<ApiService>()
        provider.request(.submitComment(postId: postId, name: name, email: email, content: content)) { (result) in
            switch result {
            case let .success(moyaResponse) :
                let json = try! moyaResponse.mapJSON() as! [String:Any]
                if let response = SubmitCommentResponse(JSON: json) {
                    print(result)
                    if response.status == "ok" {
                        completion(true)
                    } else {
                        completion(false)
                    }
                }
            case let .failure(error) :
                print("请求网络错误.:\(error.failureReason ?? "")")
                completion(false)
            }
        }
    }
    
}
